package com.damieoa.damieoa.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.damieoa.damieoa.dao.DepartmentMapper;
import com.damieoa.damieoa.pojo.Department;
import com.damieoa.damieoa.pojo.Staff;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //新建部门
    @Override
    public void addDepartment(Department department) {
        departmentMapper.insert(department);
    }

    //查询部门
    @Override
    public List<Department> queryDepartment(Department department) {
        if (department.getQuery_way().equals("department_id") ||
                department.getQuery_way().equals("department_name") ||
                department.getQuery_way().equals("department_type")) {
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(department.getQuery_way(), department.getQuery_conditions());
            return departmentMapper.selectList(queryWrapper);
        } else if (department.getQuery_way().equals("all")) {
            QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("department_id", department.getQuery_conditions())
                    .or().like("department_name", department.getQuery_conditions())
                    .or().like("department_type", department.getQuery_conditions());
            return departmentMapper.selectList(queryWrapper);
        } else {
            return null;
        }

    }

    //根据部门id获取部门信息
    @Override
    public Department getDepartmentById(Department department) {
        return departmentMapper.getDepartmentById(department);
    }

    //修改部门信息
    @Override
    public void updateDepartment(Department department) {
        departmentMapper.updateDepartment(department);
    }

    //删除部门
    @Override
    public void deleteDepartment(Department department) {
        departmentMapper.deleteDepartment(department);
    }

    //查询部门员工
    @Override
    public List<Staff> queryDepartmentStaff(Department department) {
        if (department.getQuery_way().equals("全部查询")) {
            department.setQuery_way(null);
            return departmentMapper.queryDepartmentStaff(department);
        }
        return departmentMapper.queryDepartmentStaff(department);
    }

    //查询部门删除员工
    @Override
    public void deleteDepartmentStaff(Staff staff) {
        departmentMapper.deleteDepartmentStaff(staff);
    }

}
